<template>
  <div class="saveUpdate">
    <el-dialog :close-on-click-modal="false" :title="id?'编辑公告':'新增公告'" :visible.sync="showDialog" @close="$emit('update:showUpdate', false)"
               :showUpdate="showUpdate" width="500px">
      <el-form :model="DetailInfo" ref="saveChn" label-width="120px" size="mini">
        <dialog-wrapper :MaxWidth="400">
          <el-form-item label="公告内容：">
            <el-input v-model="DetailInfo.value"></el-input>
          </el-form-item>
          <el-form-item label="播放间隔时间：">
            <el-input v-model.number="DetailInfo.play_interval"></el-input>
          </el-form-item>
          <el-form-item label="开始时间：">
            <el-date-picker :picker-options="pickerOptions" v-model="DetailInfo.start_time" value-format="yyyy-MM-dd HH:mm:ss" type="datetime" placeholder="选择日期时间"></el-date-picker>
          </el-form-item>
          <el-form-item label="结束时间：">
            <el-date-picker :picker-options="pickerOptions" v-model="DetailInfo.end_time" value-format="yyyy-MM-dd HH:mm:ss" type="datetime" placeholder="选择日期时间"></el-date-picker>
          </el-form-item>
        </dialog-wrapper>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="$emit('update:showUpdate', false)" size="small">取 消</el-button>
        <el-button v-if="id" type="primary" :loading="loading" @click="updateDetail" size="small">确 定</el-button>
        <el-button v-else type="primary" :loading="loading" @click="saveDetail" size="small">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import DialogWrapper from '@/components/DialogWrapper'
  import { getBulletinDetail, saveBulletin, updateBulletin } from '@/api'
  export default {
    components: { DialogWrapper },
    data() {
      return {
        setbank: false,
        showDialog: this.showUpdate,
        loading: false,
        DetailInfo: {},
        pickerOptions: {
          disabledDate(time) {
            return time.getTime() < Date.now() - 8.64e7
          }
        }
      }
    },
    props: {
      showUpdate: {
        type: Boolean,
        default: false
      },
      id: {
        type: Number
      },
      BulletinType: {
        type: Object
      },
      settleCycle: {
        type: Object
      },
    },
    created(){
      if(this.id){
        this.getDetail()
      }
    },
    methods: {
      saveDetail(){
        this.$refs.saveChn.validate(valid => {
          if (valid) {
            saveBulletin(this.DetailInfo).then(res => {
              if(res.status === 0){
                this.$message({ message: '新增成功。',type: 'success' })
                this.$emit('update:showUpdate', false)
              }
            })
          }
        })
      },
      updateDetail(){
        this.$refs.saveChn.validate(valid => {
          if (valid) {
            updateBulletin(this.DetailInfo, this.id).then(res => {
              if(res.status === 0){
                this.$message({ message: '编辑成功。',type: 'success' })
                this.$emit('update:showUpdate', false)
              }
            })
          }
        })
      },
      getDetail(){
        getBulletinDetail(this.id).then(res => {
          if(res.status === 0){
            this.DetailInfo = res.data
          }
        })
      }
    },
    watch: {
      showUpdate() {
        this.showDialog = this.showUpdate
      }
    }
  }
</script>
